package com.candy.db.generator.main;

import com.candy.db.generator.config.GlobalConfig;
import com.candy.db.generator.utils.StrKit;

import java.io.File;
import java.security.InvalidParameterException;
import java.util.List;

/**
 * Created by yantingjun on 2016/7/14.
 */
public class CommandParamParser {
    /**
     * 必传参数
     */
    public static CommandParam parse(String[] args){
        CommandParam commandParam = new CommandParam();
        for(int i=0;i<args.length ;i++){
            String arg = args[i];
            if("-o".equalsIgnoreCase(arg)){
                commandParam.outDir = args[++i];
            }else if("-bmn".equalsIgnoreCase(arg)){
                commandParam.baseModelPackageName = args[++i];
            }else if("-mn".equalsIgnoreCase(arg)){
                commandParam.modelPackageName = args[++i];
            }else if("-on".equalsIgnoreCase(arg)){
                commandParam.daoPackageName = args[++i];
            }else if("-bmo".equalsIgnoreCase(arg)){
                commandParam.baseModelOutDir = args[++i];
            }else if("-mo".equalsIgnoreCase(arg)){
                commandParam.modelOutDir = args[++i];
            }else if("-oo".equalsIgnoreCase(arg)){
                commandParam.daoOutDir = args[++i];
            }else if("-ex".equalsIgnoreCase(arg)){
                commandParam.excludeTables = StrKit.splitNotBlankDistinct(args[++i], ",");
            }else if("-f".equalsIgnoreCase(arg)){
                commandParam.overrideForce = true;
            }else if("-t".equalsIgnoreCase(arg)){
                commandParam.templateGroup = args[++i];
            }else{
                throw new InvalidParameterException("Invalid arg:"+arg+",please check it out!");
            }
        }
        //合法性校验
        if(StrKit.isBlank(commandParam.modelPackageName) && StrKit.isBlank(GlobalConfig.modelPackageName)){
            throw new InvalidParameterException("Missing -mn ，please check it out!");
        }
        if(StrKit.isBlank(commandParam.daoPackageName)  && StrKit.isBlank(GlobalConfig.daoPackageName)){
            throw new InvalidParameterException("Missing -on ，please check it out!");
        }
        return commandParam;
    }
}
